package com.haixiaoke.flat.contract.controller;

import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.annotation.RepeatSubmit;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.contract.domain.Contract;
import com.haixiaoke.saas.contract.service.IContractService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 合同Controller
 *
 * @author panda
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/front/flat/contract/contract")
public class FlatContractController extends BaseController {
    @Autowired
    private IContractService contractService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 查询合同列表
     */
    @AuthCheck
    @GetMapping("/page")
    public TableDataInfo list(Contract contract) {
        startPage();
        contract.setConUserId(frontTokenComponent.getUserIdNotNull());
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 获取合同详细信息
     */
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable String contractId) {
        return success(contractService.selectContractByContractId(contractId));
    }

    /**
     * 获取预览电子合同内容链接
     */
    @AuthCheck
    @PostMapping(value = "/previewUrl")
    public AjaxResult electronicVisaPreviewUrl(@RequestBody Contract contract) {
        return contractService.selectContractPreviewUrl(contract);
    }

    /**
     * 签署电子合同
     */
    @AuthCheck
    @RepeatSubmit
    @PutMapping("/signing")
    public AjaxResult signing(@RequestBody Contract contract) {
        return contractService.signingContract(contract, false);
    }
}
